package me.shiqiang.im;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.protocol.JacksonJsonSupport;
import me.shiqiang.im.config.RocketMqConsumerConfig;
import me.shiqiang.im.linsteners.Listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by maliangxue on 2018/4/15.
 */
@SpringCloudApplication
@EnableFeignClients
@EnableCaching
@EnableAutoConfiguration
@EnableScheduling
public class ImRouterServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ImRouterServerApplication.class, args);
    }


    @Value("${connect.port}")
    String port;


    @Autowired
    private Map<String,MessageListener> consumerMap;

    @Value("#{${com.tst.rocketmq.consumer.subscription}}")
    Map<String, String> consumerSubscription;


    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean createConsumerBean(RocketMqConsumerConfig config) {
        ConsumerBean consumerBean = new ConsumerBean();
        consumerBean.setProperties(config.getProperties());
        Map<Subscription, MessageListener> table = new HashMap<>();
        for (Map.Entry<String, String> consumerConfig : consumerSubscription.entrySet()) {
            if (consumerMap.containsKey(consumerConfig.getKey())) {
                MessageListener messageListener = consumerMap.get(consumerConfig.getKey());
                String[] configs = consumerConfig.getValue().split("#");
                Subscription subscription = new Subscription();
                subscription.setTopic(configs[0]);
                subscription.setExpression(configs[1]);
                table.put(subscription, messageListener);
            }
        }
        consumerBean.setSubscriptionTable(table);
        return consumerBean;
    }


    @Autowired
    private Map<String,Listener> listenerMap;

    @Bean(initMethod = "start",destroyMethod = "stop")
    public SocketIOServer createServer(){
        Configuration configuration = new Configuration();
//        configuration.setAuthorizationListener(auth);
//		configuration.setHostname("192.168.20.143");
        configuration.setPort(Integer.parseInt(port));
        SocketConfig socketConfig = configuration.getSocketConfig();
        socketConfig.setReuseAddress(true);//添加此配置，解决重启报 Address already in use异常
        JacksonJsonSupport jacksonJsonSupport = new JacksonJsonSupport();
        configuration.setJsonSupport(jacksonJsonSupport);

        SocketIOServer server = new SocketIOServer(configuration);
        for (Listener listener : listenerMap.values()) {
            listener.setServer(server);
            server.addListeners(listener);
        }
        return server;
    }

}
